package org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.read;

import java.util.ArrayList;
import java.util.List;
import org.opentcs.kcvehicle.communication.kc.common.byteutils.ByteUtil;

public class ReadStrValue {
  //变量名，16个字节( 以这个变量名来定位变量的)
  private byte[] varName;
  //成员变量数量，4个字节
  private byte[] memberVarNum;
  private byte[] memberList;

  public ReadStrValue(String varName, Integer memberVarNum, List<ReadValueMember> memberList) {
    this.varName = ByteUtil.stringTo16Byte(varName);
    this.memberVarNum = ByteUtil.intToBytes(memberVarNum);
    List<Byte> bytes = new ArrayList<>();
    for (ReadValueMember member : memberList) {
      bytes.addAll(member.toBytes());
    }
    this.memberList = new byte[bytes.size()];
    for (int i = 0; i < bytes.size(); i++) {
      this.memberList[i] = bytes.get(i);
    }
  }

  public ReadStrValue(String varName) {
    this.varName = ByteUtil.stringTo16Byte(varName);
    this.memberVarNum = ByteUtil.intToBytes(0);
    this.memberList = new byte[0];
  }

  public List<Byte> toBytes() {
    List<Byte> bytes = new ArrayList<>();
    for (byte b : varName) {
      bytes.add(b);
    }
    for (byte b : memberVarNum) {
      bytes.add(b);
    }
    for (byte b : memberList) {
      bytes.add(b);
    }
    return bytes;
  }
}
